<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    程序举例（文件） - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						程序举例（文件）
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/02/09 03:35 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p>有地图文件map.txt和符号文件shape.txt，如下图所示，编写代码将map文件中的内容替换为shape文件中对应的符号并显示出来<br />
<img src="media/16443489120605/16451233530502.jpg" alt="" /></p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#define row 10
#define column 20

struct shape
{
    char ch;
    char type[5];
};

//读取map.txt
void readmap(char map[row][column])
{
    FILE *fp;
    char c;
    fp = fopen(&quot;map.txt&quot;, &quot;r&quot;);
    if (!fp)
    {
        printf(&quot;map.txt open error&quot;);
        exit(-1); //-1表示程序异常结束，0表示程序正常结束
    }

    int i = 0, j = 0; // i代表行，j代表列
    while ((c = fgetc(fp)) != EOF)
    {
        if (c == '\n') //读取到换行符时
        {
            i++;           //行数+1
            j = 0;         //列数归零
            c = fgetc(fp); //使用fgetc()读取下一个有意义的字符
        }
        map[i][j] = c;
        j++;
    }
    fclose(fp);
};

//读取shape.txt
void readshape(struct shape s[3])
{
    FILE *fp;
    char c;
    fp = fopen(&quot;shape.txt&quot;, &quot;r&quot;);
    if (!fp)
    {
        printf(&quot;shape.txt open error&quot;);
        exit(-1); //-1表示程序异常结束，0表示程序正常结束
    }

    int i = 0; // i代表结构体结构数组的下标

    while ((fscanf(fp, &quot;%c\t%s\n&quot;, &amp;s[i].ch, s[i].type)) != EOF)
        i++;

    fclose(fp);
}

//输出map并替换为shape内的符号
void showmap(char map[row][column], struct shape s[3])
{
    int i, j, k;
    for (i = 0; i &lt; row; i++)
    {
        for (j = 0; j &lt; column; j++)
        {
            for (k = 0; k &lt; 3; k++)
                if (map[i][j] == s[k].ch)
                {
                    printf(&quot;%s&quot;, s[k].type);
                    break;
                }
        }
        putchar('\n');
    }
}

int main()
{
    char map[row][column];
    struct shape s[3];

    //读取map.txt
    readmap(map);
    readshape(s);
    showmap(map, s);

    return 0;
}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>